/**
 * 
 */
var setting = {
	async: {
		enable: true,
		dataType: "json",
		url:"/HomeAcount/menu/loadMenus",
		autoParam:["id=id"]
	},
	callback: {
		onClick: zTreeOnClick
	}
};

$(function(){
	$.fn.zTree.init($("#menuTree"), setting);
	//表单校验
	$('#menuForm').validationEngine('attach', {
		autoHidePrompt: true,
		autoHideDelay : 3000,
		onValidationComplete : function(form, status) {
			if(status == true) {
				menuSave();
			}
		}
	});
});

function zTreeOnClick(event, treeId, treeNode) {
//    var isParent = treeNod.isParent;
	var level = treeNode.level;
    if(level > 0) {
    	$.ajax({
			url : "/HomeAcount/menu/getMenuInfoById",
			dataType : "json",
			type : "post",
			data : {
				id : treeNode.id
			},
			success : function(data) {
				if(data) {
					$('#opFlag').val("2");//修改
					$('#menuForm').form('load', data);
				}else {
					$(window.parent.alert("未加载到数据！"));
				}
			},
			error : function() {
				$(window.parent.alert("加载数据出错，请联系管理员！"));
			}
		});
    }
}

function formRest() {
	$('#opFlag').val("1");//新增
	$('#menu_id').val('');
	$('#menuForm').form('reset');
}

function menuSave() {
	var data = $('#menuForm').serializeJson();
	$.ajax({
		url : "/HomeAcount/menu/menuSave",
		dataType : "json",
		type : "post",
		data : data,
		success : function(reData) {
			if(reData.flag) {
				$.fn.zTree.init($("#menuTree"), setting);
				$('#menu_parentid').empty().append('<option value="">---请选择---</option>');
				var op = "";
				var pid = reData.pMenu;
				if(reData.pMenus && reData.pMenus.length > 0) {
					for(var i = 0; i < reData.pMenus.length; i++) {
						if(pid == reData.pMenus[i].id) {
							op += '<option value="'+reData.pMenus[i].id+'" selected="selected">'+reData.pMenus[i].name+'</option>';
						}else {
							op += '<option value="'+reData.pMenus[i].id+'">'+reData.pMenus[i].name+'</option>';
						}
					}
					$('#menu_parentid').append(op);
				}
				$(window.parent.alert("操作成功！"));
			}else {
				$(window.parent.alert("系统出错，请联系管理员！"));
			}
		},
		error : function() {
			$(window.parent.alert("系统出错，请联系管理员！"));
		}
	});
}


